package com.cn.zpark.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: com.cn.zpark.servlet
 * @ClassName: ServletDemo02
 * @Description:
 * @author: RZS
 * @date: 2022/4/18  14:35
 */
@WebServlet("/user/d2.do")
public class ServletDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("<h1>doGet方法处理请求</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("<h1>doPost方法处理请求</h1>");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求方式
        String method = req.getMethod();
        // 判断请求方式，调用对应的方法进行处理
        if("GET".equals(method) || "get".equals(method)){
            // get请求
            doGet(req, resp);
        }else{
            // 调用POST请求
            doPost(req, resp);
        }
    }
}
